/*--------------------------------------------------------------------------
  Project::HEXAGON FLIGHT SIMULATOR
  Package::Scenegraph
  Class::Scenegraph
  File Scenegraph.h
  --------------------------------------------------------------------------
  * METHODS AND CLASS PARAMETERS


  --------------------------------------------------------------------------
  * FUNCTIONS DESCRIPTION
  

  --------------------------------------------------------------------------
  * COPYRIGHT

  --------------------------------------------------------------------------
  * INFO ABOUT AUTHOR AND VERSIONS
  Author:	Tino Giorgio	Data: 
  Rev1:			Data:

  --------------------------------------------------------------------------
  * E-MAIL & CONTACTS

  GIORGIO TINO:	tinoshi@libero.it
	           	shapeofangels@hotmail.com
	            giorgio.tino@studenti.polito.it

  --------------------------------------------------------------------------*/



#if !defined(SCENEGRAPH_H)
#define SCENEGRAPH_H

#include "LoadingInfo.h"
#include "Movie.h"
#include "Waypoint.h"

/* PLIB include */
#include <plib/ul.h>
#include <plib/sg.h>
#include <plib/ssg.h>
#include <plib/sl.h>
#include <plib/ssgAux.h>
#include <plib/ssgaSky.h>

#define ADD_BUILDING	1
#define ADD_TREE		0
#define SET_TEXTURE		2
#define MOVE_SCENARIO	4
#define ADD_FACILITY    3

#define ADD_WAYPOINT	0
#define DELETE_WAYPOINT	1
#define MOVE_WAYPOINT	2
#define INSERT_WAYPOINT 3

#define ADD_MOVIE    	0
#define DELETE_MOVIE	1
#define MOVE_MOVIE		2
#define INSERT_MOVIE    3

#define X_POSITION 0
#define Y_POSITION 1
#define Z_POSITION 2
#define H_POSITION 3
#define P_POSITION 4
#define R_POSITION 5

class Camera;
class Land;
class sgdLineSegment3;
class ObjectList;
class Waypoint;
class Movie;
class Sky;
class LensFlare;

class Scenegraph  
{
public:
	Scenegraph(LoadingInfo *li, Camera *c);
	virtual ~Scenegraph();

	void addLand(Land* land);
	void addSky();
	void addWaypoints(char filename[]);
	void addMovie(char filename[]);
	void display();
	void createStates();
	void stopAction();

	void switchToEditingView();
	void switchToCameraRepositioningView();
	void switchToMovieView();

	void testLOS(sgdLineSegment3* ls, int type, int kind);
	void showObject(sgdLineSegment3* ls, int type, int kind);

	void updateObjectPosition(int what, float howMuch);

	ObjectList* getList(int type);
	Waypoint*   getWaypoints();
	Movie*      getMovie();

	void setWireframe(int s);

	void load();
	void printTr();

	float getHOT(float x, float y);

	float* getPointerPosition();

	float getHeigth(float x, float y);

	void hideWaypoints();
	void addWorld();

	void setCameraStartPosition(float x, float y, float z, float h, float p, float r);

private:
    ssgRoot* root;

	ssgTransform* landTransform;
	ssgTransform* pineTreeModel;
	ssgTransform* houseTransform;
	ssgTransform* houseModel;
	ssgTransform* skyTransform; 
	ssgSimpleState* createNewState(int t);
	ssgSimpleState* loadTextureState(int kind);

	Land *land;
	Sky  *sk;
	LensFlare *lf;

	Camera *camera;

	ulClock *ck;

	ssgEntity* loadTree();
	void addTree(float x, float y, float z);
	ssgEntity* loadBuilding();
	void addBuilding(float x, float y, float z);
	void addTexture(int kind, float x, float y, float z);
	void selectObject(int type, int kind, float x, float y, float z);
	void repositionObject(float x, float y, float z);
	bool selectWaypoint(ssgHit *h);
	int selectMovie(ssgHit *h);
	Frame* selectMovieLine(ssgHit *h);
	WPCoord* selectWaypointLine(ssgHit *h);
	void deselectMovie();


	void loadObjects();

	ssgTransform* addObject(int type, int kind, float x, float y, float z, float h, float p, float r);
	float findNearestTerrainLeaf(sgdLineSegment3* ls);
	float findNearestMovieLine(sgdLineSegment3* ls);

	ObjectList *buildingList;
	ObjectList *treeList;
	ObjectList *textureList;
	ObjectList *facilityList;

	LoadingInfo *info;

	ulLinkedList *buildingModelsList;
	ulLinkedList *treeModelsList;
	ulLinkedList *facilityModelsList;

	Waypoint *wp;
	Movie    *mv;

	int objectType;
	int objectKind;
	sgCoord objectPosition;
	ssgTransform* showObjectTransform;
	ssgTransform* new_object_transform;
	ssgTransform* old_object_transform;

	sgVec3 point;
	sgVec3 movie_line_point;

	WPCoord* selectedWaypoint;
	bool movingWaypoint;
	bool movingMovie;

	Frame* selectedMovie;
	sgVec3 nearest_point;

	ssgLeaf *nearestLeaf;
	int movieSelected;

	float x_correction;
	float y_correction;

	sgCoord cameraStartPosition;
};

#endif // !defined(SCENEGRAPH_H)
